Struct Vs Class When To Use Which · How it works

1 min read
Mid-level7 min read
Rapid overview

How it works

_TODO: explain the mental model._

🧩 Real-World Example (HFM context)

If you’re processing millions of tick messages per second:

  • Use a struct (or readonly struct) for individual ticks (lightweight, immutable).
  • Use a class for services and entities that manage state, like OrderBook, TradeSession, or CacheManager.

Example:

readonly struct Tick
{
    public string Symbol { get; init; }
    public double Bid { get; init; }
    public double Ask { get; init; }
}

class PriceFeedProcessor
{
    private readonly List<Tick> _ticks = new();

    public void OnTick(Tick tick) => _ticks.Add(tick);
}

See also